package com.example.qxfw.rcyw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.example.qxfw.common.entity.yj.ThunderMoniInfo;
import com.example.qxfw.rcyw.mapper.ThunderMoniInfoMapper;
import com.example.qxfw.rcyw.service.ThunderMoniInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

@Service
public class ThunderMoniInforServiceImpl implements ThunderMoniInfoService {

    @Autowired
    private ThunderMoniInfoMapper thunderMoniInfoMapper;

    @Override
    public List<ThunderMoniInfo> getThunderMonitor(Integer thunderAutoHour) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - thunderAutoHour);
        Timestamp observetime = Timestamp.valueOf(sdf.format(calendar.getTime()));
        LambdaQueryWrapper<ThunderMoniInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.gt(ThunderMoniInfo::getObservetime,observetime).eq(ThunderMoniInfo::getIsremoved,false);
        List<ThunderMoniInfo> thunderMoniInfos = thunderMoniInfoMapper.selectList(queryWrapper);
        return thunderMoniInfos;
    }

    @Override
    public Integer updateThunderMonitor(String ids) {
        int count = 0;
        String[] idarr = ids.split(",");
        for (int k = 0; k < idarr.length; k++) {
            ThunderMoniInfo info = new ThunderMoniInfo();
            info.setIsremoved(true);
            LambdaUpdateWrapper<ThunderMoniInfo> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(ThunderMoniInfo::getId, Integer.parseInt(idarr[k]));
            count += thunderMoniInfoMapper.update(info, updateWrapper);
        }
        return count;
    }
}
